home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_539 / rpn / source / mainloop.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  166 lines

  1. /*-------------------------------------*
  2.  | File: MAINLOOP.c - MLO 900131 V1.00 |
  3.  | Here is RPN main loop               |
  4.  *-------------------------------------*/
  5.  
  6. #include "rpn.h"
  7. #include "proto.h"
  8. #include "mainloop.h"
  9. #include <ctype.h>
  10.  
  11. extern struct Window *Wrpn;
  12. extern struct Window *Wreg;
  13. extern struct Gadget *pIG;
  14. extern struct StringInfo *pIS;
  15. extern Boolean MathError;
  16. extern int LastCode;
  17. extern char InBuf[];
  18. extern double stack[];
  19.  
  20. static Boolean readInput(void);
  21.  
  22. void mainloop(void)
  23. {/*-------------------------------------*
  24.   | Wait for an Intuition message, then |
  25.   | calls the appropriate processor     |
  26.   *-------------------------------------*/
  27.  
  28.   struct IntuiMessage *pIM;
  29.   struct Gadget *addr;
  30.   ULONG class;
  31.   USHORT code;
  32.   Boolean numInput = False;
  33.  
  34. /*--------------------------------------*
  35.  | Waiting for an Intuition message ... |
  36.  *--------------------------------------*/
  37.  
  38.   FOREVER {
  39.     Wait(1 << Wrpn->UserPort->mp_SigBit);
  40.     while ( (pIM = (struct IntuiMessage *) GetMsg(Wrpn->UserPort))
  41.              != NULL) {
  42.               
  43. /*-----------------------------------------------------------*
  44.  | Cleanup of the error flag, then look at the message: pick |
  45.  | up type, code and address, and reply as soon as possible. |
  46.  *-----------------------------------------------------------*/
  47.  
  48.       MathError = False;
  49.       class = pIM->Class;
  50.       code = pIM->Code;
  51.       addr = (struct Gadget *) pIM->IAddress;
  52.       ReplyMsg(pIM);
  53.       
  54.       switch (class) {
  55.  
  56. /*----------------------*
  57.  | Request to terminate |
  58.  *----------------------*/
  59.  
  60.         case CLOSEWINDOW:
  61.           return;
  62.  
  63. /*-------------------------------*
  64.  | Menu; if a number was input   |
  65.  | read it, then call menupick() |
  66.  *-------------------------------*/
  67.  
  68.         case MENUPICK:
  69.           if (numInput) {
  70.             numInput = False;
  71.             if (!readInput())   break;
  72.           }
  73.           menupick(code);
  74.           break;
  75.  
  76. /*---------------------------------------------------*
  77.  | A Gadget was hit. If it is the Input Field, input |
  78.  | a number; else, if a number was input read it.    |
  79.  *---------------------------------------------------*/
  80.  
  81.         case GADGETDOWN:
  82.           if (addr == pIG) {
  83.             numInput = True;
  84.           } else if (numInput) {
  85.             numInput = False;
  86.             readInput();
  87.           }
  88.           break;
  89.  
  90. /*--------------------------------------------------------*
  91.  | A Gadget was released. If a number was input, read it; |
  92.  | then perform the appropriate action calling keypick()  |
  93.  *--------------------------------------------------------*/
  94.  
  95.         case GADGETUP:
  96.           if (numInput) {
  97.             numInput = False;
  98.             if (!readInput())   break;
  99.           }
  100.           keypick(addr->GadgetID);
  101.           break;
  102.  
  103. /*-----------------------------------------------------*
  104.  | A keyboard key was hit; of course the Input string  |
  105.  | gadget is NOT selected. If this key is suitable for |
  106.  | numerical input, activate the input field gadget.   |
  107.  *-----------------------------------------------------*/
  108.  
  109.         case VANILLAKEY:
  110.           if (isdigit(code)   ||   code == '.'   ||   code == 'e'   ||
  111.                 code == 'E'   ||   code == '+'   ||   code == '-') {
  112.             InBuf[0] = code;
  113.             InBuf[1] = '\0';
  114.             pIS->BufferPos = 1;
  115.             pIS->DispPos = 0;
  116.             if (ActivateGadget(pIG, Wrpn, NULL)) {
  117.               RefreshGList(pIG, Wrpn, NULL, 1);
  118.               numInput = True;
  119.             }
  120.           }
  121.           break;
  122.  
  123.         default:
  124.           fprintf(stderr, "Unknown message for Wrpn, class 0x%X\n", class);
  125.           break;
  126.       }
  127.     }
  128.   }
  129. }
  130.  
  131. static Boolean readInput(void)
  132. {/*-----------------------------------------------------*
  133.   | Local function: read a number from the input field, |
  134.   | and display a requester if some error is detected.  |
  135.   *-----------------------------------------------------*/
  136.  
  137.   double x;
  138.   char dummy;
  139.  
  140.   struct IntuiText invalid = {
  141.     BLUE_PEN, WHITE_PEN, JAM2, INV_X, INV_Y, NULL,
  142.     "Invalid numeric input:", NULL
  143.   };
  144.  
  145.   struct IntuiText field = {
  146.     BLUE_PEN, WHITE_PEN, JAM2, FIE_X, FIE_Y, NULL,
  147.     InBuf, NULL
  148.   };
  149.  
  150.   struct IntuiText ok = {
  151.     BLUE_PEN, WHITE_PEN, JAM2, OK_X, OK_Y, NULL,
  152.     "OK", NULL
  153.   };
  154.  
  155.   if (sscanf(InBuf, "%lg%c", &x, &dummy) == 1) {
  156.     impEnter();
  157.     stack[0] = x;
  158.     outStk();
  159.     return True;
  160.   } else {
  161.     invalid.NextText = &field;
  162.     AutoRequest(Wrpn, &invalid, NULL, &ok, NULL, NULL, INV_W, INV_H);
  163.     return False;
  164.   }
  165. }
  166.